package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.RotationRecordsMapper;
import com.zhijian.medical.entity.po.RotationRecords;
import com.zhijian.medical.entity.vo.request.RotationRecordsSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.RotationRecordsSaveVo;
import com.zhijian.medical.entity.vo.request.RotationRecordsSearchVo;
import com.zhijian.medical.entity.vo.response.RotationRecordsVo;
import com.zhijian.medical.service.RotationRecordsService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 职工个人轮岗记录业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 14:20:26
 */
@Slf4j
@Service
public class RotationRecordsServiceImpl implements RotationRecordsService {

	@Autowired
    private RotationRecordsMapper mapper;

    @Override
    public RotationRecords selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RotationRecordsVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RotationRecords rotationRecords) {
        Date now = new Date();
        rotationRecords.setCreateTime(now);
        rotationRecords.setUpdateTime(now);
        return mapper.insert(rotationRecords) > 0 ? rotationRecords.getId() : 0L;
    }

    @Override
    public Long insertSelective(RotationRecords rotationRecords) {
        Date now = new Date();
        rotationRecords.setCreateTime(now);
        rotationRecords.setUpdateTime(now);
        return mapper.insertSelective(rotationRecords) > 0 ? rotationRecords.getId() : 0L;
    }

    @Override
    public boolean update(RotationRecords rotationRecords) {
        Date now = new Date();
        rotationRecords.setUpdateTime(now);
        return mapper.updateByPrimaryKey(rotationRecords) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RotationRecords record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RotationRecordsSaveOrUpdateVo saveOrUpdateVo) {
        RotationRecords rotationRecords = new RotationRecords();
        BeanUtils.copyProperties(saveOrUpdateVo, rotationRecords);
        return this.insert(rotationRecords) != 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean saveList(RotationRecordsSaveVo saveOrUpdateVo) {
        // 先删除原有的数据
        this.deleteByCondition(RotationRecordsSearchVo.builder().personId(saveOrUpdateVo.getPersonId()).build());
        // 再新增新的数据
        for (RotationRecordsSaveOrUpdateVo item : saveOrUpdateVo.getRecords()) {
            item.setPersonId(saveOrUpdateVo.getPersonId());
            item.setPersonGroup(saveOrUpdateVo.getPersonGroup());
            item.setPersonName(saveOrUpdateVo.getPersonName());
            item.setPersonTitle(saveOrUpdateVo.getPersonTitle());
            this.save(item);
        }
        return true;
    }

    @Override
    public boolean modify(RotationRecordsSaveOrUpdateVo saveOrUpdateVo) {
        RotationRecords old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RotationRecords rotationRecords = new RotationRecords();
        BeanUtils.copyProperties(saveOrUpdateVo, rotationRecords);
        return this.update(rotationRecords);
    }

    @Override
    public RotationRecordsVo selectOne(RotationRecordsSearchVo rotationRecordsSearchVo) {
        return mapper.selectOne(rotationRecordsSearchVo);
     }

    @Override
    public Integer count(RotationRecordsSearchVo rotationRecordsSearchVo) {
        return mapper.count(rotationRecordsSearchVo);
    }

    @Override
    public List<RotationRecordsVo> selectList(RotationRecordsSearchVo rotationRecordsSearchVo) {
        return mapper.selectList(rotationRecordsSearchVo);
    }

    @Override
    public Page<RotationRecordsVo> selectPage(RotationRecordsSearchVo rotationRecordsSearchVo) {
        AssertUtil.check(Objects.isNull(rotationRecordsSearchVo.getPageNum()) || Objects.isNull(rotationRecordsSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(rotationRecordsSearchVo);
        //分页
        Page<RotationRecordsVo> page = new Page<>(true, count, rotationRecordsSearchVo.getPageNum(), rotationRecordsSearchVo.getPageSize());
        rotationRecordsSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RotationRecordsVo> responseVos = this.selectList(rotationRecordsSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RotationRecords> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RotationRecordsSearchVo rotationRecordsSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(rotationRecordsSearchVo);
    }
    
}